package com.hxy.springcloud.controller;

import com.hxy.springcloud.pojo.Department;
import com.hxy.springcloud.service.DepartnentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class DepartnentController {
    @Autowired
    private DepartnentService departnentService;

    //获取一些配置的信息来得到具体的微服务
    @Autowired
    private DiscoveryClient client;

    @PostMapping("/dept/add")
    public boolean addDepartment(Department department) {
        return departnentService.addDepartment(department);
    }

    @GetMapping("/dept/queryById/{id}")
    public Department queryDepartmentById(@PathVariable("id") long id) {
        return departnentService.queryDepartmentById(id);
    }

    @GetMapping("/dept/queryAll")
    public List<Department> queryAllDepartment() {
        List<Department> list = departnentService.queryAllDepartment();
        return list;
    }

    //注册进来的微服务获取一些消息
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery-service:" + services);

        //通过具体的服务ID获取服务信息
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPARTMENT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost() + "\t"
            +instance.getPort() + "\t"
            +instance.getUri() + "\t"
            +instance.getServiceId());
        }

        return this.client;
    }

}
